/**
 * Copyright (c) 2020 libinbin000
 * SFrame is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

import * as mysql from "mysql";
import {Pool} from "mysql";

export class DB{
    static isInit:boolean = false;
    static pool:Pool;
    
    static init(){
        if(!this.isInit){
            this.pool = mysql.createPool({
                connectionLimit: 100,
                host : "localhost",
                user : "root",
                password : "123456",
                database : "test",
                port : 3306
            });
            // this.pool.on('connection', function (connection) {
            //     console.log("数据库连接成功");
            // });
            this.isInit = true;
        }
    }

    static query(sql:string, callback:Function){
        this.pool.query(sql, function (error, results, fields) {
            if (error) throw error;
            callback(error, results, fields);
        });
    }
}